import wx
from PublicMethod import *
from Constant import *
'''
ScoreBoard : 

    The score board records as follow :
    
    1. step name
    2. current eaten number of diamond / target number of diamond
    3. the rest of the time (minute : second)
    4. the rest of life of player
    5. the current got score
'''
class ScoreBoard():
    '''
    Constructor : ScoreBoard
        mainMap - object pointer MainMap
        panel - this object is added to panel
        startPos - position of the score board (x, y)
        isRecordScore - record whether system record score to file as game ends
    '''
    def __init__(self, mainMap, panel, startPos, isRecordScore):
        self._001_initialInstanceVariable(mainMap, panel, startPos, isRecordScore)
        self._002_bindAction()
        self._003_createBorder()
        self._004_createImageLabel()
        self._005_createEachText()
        self.ACT002_setScoreToZero()
        if self.isRecordScore :
            self.SET004_playerLife(INITIAL_LIFE)
        else :
            self.SET004_playerLife(-1)
    '''
    function _001 : initialize the variables used for this object
        mainMap - object pointer MainMap
        panel - this object is added to panel
        startPos - position of the score board (x, y)
        isRecordScore - record whether system record score to file as game ends
    '''
    def _001_initialInstanceVariable(self, mainMap, panel, startPos, isRecordScore):
        #object pointer - MainMap
        self.mainMap = mainMap
        #panel of add object
        self.panel = panel
        #start position of score board
        self.startPos = startPos
        #record whether system records score
        self.isRecordScore = isRecordScore
        #list of border
        self.list_border = None
        #image list of each label
        self.list_imgLabel = []
        #text of step
        self.textStep = None
        #text of time
        self.textTime = None
        #text of diamond information
        self.textDiamondInfo = None
        #text of player life
        self.textLife = None
        #text of score
        self.textScore = None
        #timer of counting from end
        self.timer_countDown = wx.Timer()
        #timer of adding each second to score gradually
        self.timer_addSecToScore = wx.Timer()
        #record current step name (str)
        self.stepName = None
        #record current rest time (minute, second)
        self.restTime = [0,0]
        #record current diamond information(eaten number, target number)
        self.diamondInfo = [0,0]
        #record current life of player
        self.playerLife = 0
        #record current score
        self.score = 0
    '''
    function _002 : bind object event to specific method
    '''
    def _002_bindAction(self):
        self.timer_countDown.Bind(wx.EVT_TIMER, self.SLOT001_countFromEnd)
        self.timer_addSecToScore.Bind(wx.EVT_TIMER, self.SLOT002_addSecToScore)
    '''
    function _003 : create border for score board
    '''
    def _003_createBorder(self):
        self.list_border = PublicMethod.ACT006_createABorder(self.panel, self.startPos, SIZE_BOARD, 5, "green")
    '''
    function _004 : create image for each title
    '''
    def _004_createImageLabel(self):
        list_img = ["Step.png", "time.png", "diamond.png", "life.png", "score.png", ]
        interval = ((SIZE_BOARD[1] - 15) / list_img.__len__())
        curPos = [self.startPos[0] + 15, self.startPos[1] + 30]
        index = 0
        for imgName in list_img :
            fullName = DIR_IMG_SCORE_BOARD + "/" + imgName
            image = PublicMethod.ACT001_getBmpImage(fullName, SIZE_IMG_LABEL[0], SIZE_IMG_LABEL[1])
            btn = wx.BitmapButton(self.panel, -1, image, pos = curPos, style = wx.FRAME_SHAPED)
            btn.Enable(False)
            self.list_imgLabel.append(btn)
            curPos[1] = curPos[1] + interval
    '''
    function _005 : create text object for each title
    '''
    def _005_createEachText(self):
        posX = self.list_imgLabel[0].GetPosition()[0]
        textSize = 20
        #create text of recording step information
        posStep = (posX + self.list_imgLabel[0].GetSize()[0] + 10, self.list_imgLabel[0].GetPosition()[1])
        self.textStep = PublicMethod.ACT007_createLabel(self.panel, "", "cyan", 12, posStep, 
                                                        (150, SIZE_IMG_LABEL[1] + 10))
        self.textStep.Show(False)
        self.textStep.Show(True)
        #create text of recording time information
        posStep = (posStep[0], self.list_imgLabel[1].GetPosition()[1] + 20)
        self.textTime = PublicMethod.ACT007_createLabel(self.panel, "", "cyan", textSize, posStep, 
                                                        (150, 30))
        self.textTime.Show(False)
        self.textTime.Show(True)
        #create text of recording diamond information
        posStep = (posStep[0], self.list_imgLabel[2].GetPosition()[1] + 20)
        self.textDiamondInfo = PublicMethod.ACT007_createLabel(self.panel, "", "cyan", textSize, posStep, 
                                                               (150, 30))
        self.textDiamondInfo.Show(False)
        self.textDiamondInfo.Show(True)
        #create text of recording player life
        posStep = (posStep[0], self.list_imgLabel[3].GetPosition()[1] + 20)
        self.textLife = PublicMethod.ACT007_createLabel(self.panel, "", "cyan", textSize, posStep, 
                                                               (150, 30))
        self.textLife.Show(False)
        self.textLife.Show(True)
        #create text of recording score
        posStep = (posStep[0], self.list_imgLabel[4].GetPosition()[1] + 20)
        self.textScore = PublicMethod.ACT007_createLabel(self.panel, "", "cyan", textSize, posStep, 
                                                         (150, 30))
        self.textScore.Show(False)
        self.textScore.Show(True)
    '''
    function SET001 : set instance variable - stepName
    '''
    def SET001_stepName(self, stepName):
        self.stepName = stepName
        self.textStep.SetLabel(stepName)
    '''
    function SET002 : set instance variable - restTime
    '''
    def SET002_restTime(self, restTime):
        self.restTime[0] = restTime[0]
        self.restTime[1] = restTime[1]
        if self.restTime[1] < 10 :
            strSec = "0" + str(self.restTime[1])
        else :
            strSec = str(self.restTime[1])
        self.textTime.SetLabel(str(self.restTime[0]) + " : " + strSec)
    '''
    function SET003 : set instance variable - diamondInfo
    '''
    def SET003_diamondInfo_target(self, target):
        self.diamondInfo[0] = 0
        self.diamondInfo[1] = target
        self.textDiamondInfo.SetLabel(str(self.diamondInfo[0]) + " / " + str(self.diamondInfo[1]))
    '''
    function SET004 : set instance variable - playerLife
    '''
    def SET004_playerLife(self, life):
        self.playerLife = life
        if life == -1 :
            self.textLife.SetLabel("None")
        else :
            self.textLife.SetLabel(str(life))
    '''
    function GET001 : get instance variable - isRecordScore
    '''
    def GET001_isRecordScore(self):
        return self.isRecordScore
    '''
    function GET002 : get instance variable - playerLife
    '''
    def GET002_playerLife(self):
        return self.playerLife
    '''
    function GET003 : get instance variable - score
    '''
    def GET003_score(self):
        return self.score
    '''
    function ACT001 : add one to current eaten number of diamond
    '''
    def ACT001_addOneToDiamondEatenNum(self):
        self.diamondInfo[0] = self.diamondInfo[0] + 1
        self.textDiamondInfo.SetLabel(str(self.diamondInfo[0]) + " / " + str(self.diamondInfo[1]))
        return (self.diamondInfo[0] == self.diamondInfo[1])
    '''
    function ACT002 : set score to zero
    '''
    def ACT002_setScoreToZero(self):
        self.score = 0
        self.textScore.SetLabel(str(self.score))
    '''
    function ACT003 : add number - addNum to score
    '''
    def ACT003_addScore(self, addNum):
        self.score = self.score + addNum
        self.textScore.SetLabel(str(self.score))
    '''
    function ACT004 : start timer of counting from end
    '''
    def ACT004_startCountDown(self):
        self.timer_countDown.Start(1000)
    '''
    function ACT005 : stop timer of counting from end
    '''
    def ACT005_stopCountDown(self):
        self.timer_countDown.Stop()
    '''
    function ACT006 : start timer of add time to score one by one
    '''
    def ACT006_startAddSecToScore(self):
        self.timer_addSecToScore.Start(20)
    '''
    function ACT007 : destroy all object of this class
    '''
    def ACT007_destroy(self):
        #clear border list
        index = 0
        while index < self.list_border.__len__() :
            self.list_border[index].Destroy()
            index = index + 1
        #clear image list
        index = 0
        while index < self.list_imgLabel.__len__() :
            self.list_imgLabel[index].Destroy()
            index = index + 1
        #clear text object
        self.textDiamondInfo.Destroy()
        self.textScore.Destroy()
        self.textStep.Destroy()
        self.textLife.Destroy()
        self.textTime.Destroy()
        #clear timer
        self.timer_addSecToScore.Stop()
        self.timer_addSecToScore.Destroy()
        self.timer_countDown.Stop()
        self.timer_countDown.Destroy()
    '''
    function ACT008 : add number - num to life of player
    '''
    def ACT008_addOrSubPlayerLife(self, num):
        if self.playerLife != -1 :
            self.SET004_playerLife(self.playerLife + num)
    '''
    function SLOT001 : slot of counting from end
    '''
    def SLOT001_countFromEnd(self, event):
        if self.restTime[1] > 0 :  #the second is larger than zero
            self.restTime[1] = self.restTime[1] - 1
        elif self.restTime[0] > 0 and self.restTime[1] <= 0 : #the minute is larger than zero but second is small equal to zero
            self.restTime[1] = 59
            self.restTime[0] = self.restTime[0] - 1
        else :
            if self.timer_countDown.IsRunning() :  #time is up
                wx.PostEvent(self.mainMap, NewEvt_gameEnd(EndType = END_GAME_TIME_UP))
            self.timer_countDown.Stop()
        self.SET002_restTime(self.restTime)
    '''
    function SLOT002 : slot of adding rest of time to score one by one
    '''
    def SLOT002_addSecToScore(self, event):
        self.SLOT001_countFromEnd(None)
        if self.restTime[0] == 0 and self.restTime[1] == 0 :
            self.timer_addSecToScore.Stop()
            self.mainMap.ACT013_countDownAddScoreFinish()
        else :
            self.ACT003_addScore(5)